#ifndef _MY_GUI_H_
#define _MY_GUI_H_

/*********************
 *      INCLUDES
 *********************/
#include <lvgl.h>
#include <math.h>

// #include <lv_demo.h>

#include "../dev_port/lv_port_indev.h"

static void event_handler1(lv_event_t *e) {
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t *obj = lv_event_get_target(e);
  if (code == LV_EVENT_VALUE_CHANGED) {
    char buf[32];
    lv_roller_get_selected_str(obj, buf, sizeof(buf));
    LV_LOG_USER("Selected month: %s\n", buf);
  }
}

/**
 * An infinite roller with the name of the months
 */
void lv_example_roller_1(void) {
  lv_group_set_default(group);
  lv_obj_t *roller1 = lv_roller_create(lv_scr_act());
  lv_roller_set_options(roller1,
                        "January\n"
                        "February\n"
                        "March\n"
                        "April\n"
                        "May\n"
                        "June\n"
                        "July\n"
                        "August\n"
                        "September\n"
                        "October\n"
                        "November\n"
                        "December",
                        LV_ROLLER_MODE_INFINITE);

  lv_roller_set_visible_row_count(roller1, 3);
  lv_obj_center(roller1);
  lv_obj_add_event_cb(roller1, event_handler1, LV_EVENT_ALL, NULL);
  lv_obj_align(roller1, LV_ALIGN_RIGHT_MID, -10, 0);
  lv_group_add_obj(group, roller1);
}

static void event_handler2(lv_event_t *e) {
  lv_event_code_t code = lv_event_get_code(e);

  if (code == LV_EVENT_CLICKED) {
    LV_LOG_USER("Clicked");
  } else if (code == LV_EVENT_VALUE_CHANGED) {
    LV_LOG_USER("Toggled");
  }
}

void lv_example_btn_1(void) {
  lv_obj_t *label;

  lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
  lv_obj_add_event_cb(btn1, event_handler2, LV_EVENT_ALL, NULL);
  lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 10, 20);

  label = lv_label_create(btn1);
  lv_label_set_text(label, "Button");
  lv_obj_center(label);

  lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
  lv_obj_add_event_cb(btn2, event_handler2, LV_EVENT_ALL, NULL);
  lv_obj_align(btn2, LV_ALIGN_BOTTOM_LEFT, 10, -20);
  lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
  lv_obj_set_height(btn2, LV_SIZE_CONTENT);

  label = lv_label_create(btn2);
  lv_label_set_text(label, "Toggle");
  lv_obj_center(label);

  lv_group_add_obj(group, btn1);
  lv_group_add_obj(group, btn2);
}

static void slider_event_cb(lv_event_t *e);
static lv_obj_t *slider_label;

/**
 * A default slider with a label displaying the current value
 */
void lv_example_slider_1(void) {
  /*Create a slider in the center of the display*/
  lv_obj_t *slider = lv_slider_create(lv_scr_act());
  lv_obj_center(slider);
  lv_obj_set_width(slider, 200);
  lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
  lv_slider_set_value(slider, ((float)ledcRead(10) / 1023) * 100, LV_ANIM_ON);

  /*Create a label below the slider*/
  slider_label = lv_label_create(lv_scr_act());

  lv_fs_file_t f;
  lv_fs_open(&f, "F:/liuzewen.txt", LV_FS_MODE_RD);
  uint32_t read_num;
  char buf1[20];
  String data = "";
  lv_fs_read(&f, buf1, 20, &read_num);
  for (int i = 0; i < read_num; i++) {
    data += buf1[i];
  }

  Serial.printf("F:/liuzewen.txt 文件存在！读取内容为：%s ,内容长度为：%d \n",
                data.c_str(), read_num);
  lv_fs_close(&f);
  lv_label_set_text(slider_label, data.c_str());

  char buf[8];
  lv_snprintf(buf, sizeof(buf), "%d%%", lv_slider_get_value(slider));
  ledcWrite(10, lv_slider_get_value(slider) * 10 + 23);
  // lv_label_set_text(slider_label, buf);

  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

  lv_group_add_obj(group, slider);
}

static void slider_event_cb(lv_event_t *e) {
  lv_obj_t *slider = lv_event_get_target(e);
  char buf[8];
  lv_snprintf(buf, sizeof(buf), "%d%%", lv_slider_get_value(slider));
  ledcWrite(10, lv_slider_get_value(slider) * 10 + 23);
  lv_label_set_text(slider_label, buf);
  lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
}

void lv_example_colorwheel_1(void) {
  lv_obj_t *cw;

  cw = lv_colorwheel_create(lv_scr_act(), true);
  lv_obj_set_size(cw, 200, 200);
  lv_obj_center(cw);
  lv_group_add_obj(group, cw);
}

#endif /*LV_TEST_CHART_H*/
